import cv, cv2
import string
from gui import Frame_Principal as miFP
import gui
import wx
import numpy as np
import matplotlib.pyplot as plt

class MiFrame (miFP):
    def __init__(self,fps = 15):
        miFP.__init__(self, None )
        self.WinFrame = miFP
        self.capture = cv2.VideoCapture(0)
        self.capture.set(cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
        self.capture.set(cv.CV_CAP_PROP_FRAME_HEIGHT, 1024)
        ret, frame = self.capture.read()
        
        height, width = frame.shape[:2]
        self.panelDerecho.SetSize((width, height))
        self.panelIzquierdo.SetSize((width, height))
        
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

        self.bmp    = wx.BitmapFromBuffer(width, height, frame)
        self.bmpraw = wx.BitmapFromBuffer(width, height, frame)
        
        self.size_PD = self.panelIzquierdo.GetSize()
        self.size_PI  = self.panelDerecho.GetSize()

        self.timer = wx.Timer(self)
        self.timer.Start(1000./15)

        self.panelIzquierdo.Bind(wx.EVT_PAINT,self.OnPaintIz)
        self.panelDerecho.Bind(wx.EVT_PAINT, self.OnPaint)
        self.Bind(wx.EVT_TIMER, self.NextFrame)
        
    def OnPaintIz(self, evt):
        dc = wx.BufferedPaintDC(self.panelIzquierdo)
        dc.DrawBitmap(self.bmpraw, 0, 0)
    
    def OnPaint(self, evt):
        dc = wx.BufferedPaintDC(self.panelDerecho)
        dc.DrawBitmap(self.bmp, 0, 0)
        
    def NextFrame(self, event):
        ret, img2 = self.capture.read()
        img3 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
        frame = cv2.Canny(img3,self.m_slider_min.GetValue(),self.m_slider_max.GetValue())
        if ret:
            frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
            self.bmpraw.CopyFromBuffer(cv2.cvtColor(img2, cv2.COLOR_BGR2RGB))
            self.bmp.CopyFromBuffer(frame)
            self.panelDerecho.Refresh()
            self.panelIzquierdo.Refresh()
            self.imagen = img2
               
        event.Skip()


aplicacion = wx.PySimpleApp()
frame_usuario = MiFrame()
frame_usuario.Show()
aplicacion.MainLoop()
